package jt.classic.app.gdzc.riturn;

import org.limp.basework.MachiningException;
import org.limp.basework.SimpleBean;
import org.limp.basework.SimpleBean4Table;
import org.limp.basework.SimpleBeanMachining;
import org.limp.basework.Table;
import org.limp.basework.impl.SimpleBean4TableImpl;
import org.limp.basework.tools.BaseworkUtil;

import org.limp.mine.DataTrimmer;
import org.limp.mine.DateTrimmer;

import java.sql.Connection;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;


/**
 * 资产归还数据处理类
 *
 * @author mido
 *
 */
public class ReturnMachining implements SimpleBeanMachining {
    public SimpleBean machining(SimpleBean bean, Table table,
        HttpServletRequest request, Connection conn, int type)
        throws MachiningException {
        // TODO Auto-generated method stub

        /*归还资产*/
        if (table.getName().equals("APP_GDZC_LY") && (type == 1)) {
            String pkid = (String) bean.getResource().get("PKID");

            /*资产领用信息*/
            String sql = "SELECT * FROM APP_GDZC_LY WHERE PKID = " + pkid;
            DataTrimmer trimmer = new DataTrimmer(conn);
            Map lyinfo = trimmer.searchSingleData(sql);

            String gzid = (String) lyinfo.get("GZID");

            sql = "SELECT * FROM APP_GDZC WHERE GZID = '" + gzid + "'";

            Map gzinfo = trimmer.searchSingleData(sql);

            SimpleBean4Table sbt = new SimpleBean4TableImpl("APP_GDZC");
            sbt.getResource().put("GZID", gzid);
            sbt.getResource().put("GZSTATUS", "0");
            sbt.getResource().put("CKSTATUS", "1");

            BaseworkUtil util = new BaseworkUtil();

            try {
                util.manualUpdate1(sbt, conn);
            } catch (Exception ex) {
                throw new MachiningException("Return machining error.", ex);
            }

            SimpleBean4Table sbt1 = new SimpleBean4TableImpl(
                    "APP_GDZC_CKRECORD");
            sbt1.getResource().put("CKID", gzinfo.get("CKID"));
            sbt1.getResource().put("USERID", lyinfo.get("SYUSERID"));
            sbt1.getResource().put("USERNAME", lyinfo.get("SYUSERNAME"));
            sbt1.getResource().put("GROUPID", lyinfo.get("SYGROUPID"));
            sbt1.getResource().put("GROUPNAME", lyinfo.get("SYGROUPNAME"));
            sbt1.getResource().put("RKDATE", DateTrimmer.getYMDHMS());
            sbt1.getResource().put("GZID", lyinfo.get("GZID"));
            sbt1.getResource().put("GZNAME", lyinfo.get("GZNAME"));
            sbt1.getResource().put("TYPENAME", lyinfo.get("TYPENAME"));
            sbt1.getResource().put("GZCODE1", lyinfo.get("GZCODE"));
            sbt1.getResource().put("RKCOUNT", lyinfo.get("LYCOUNT"));
            sbt1.getResource().put("RKDESC", "归还入库");
            sbt1.getResource().put("RKTYPE", "0");

            try {
                util.manualSave1(sbt1, conn);
            } catch (Exception ex) {
                throw new MachiningException("Return machining error.", ex);
            }
        }

        return null;
    }
}
